package com.example.poemtest.index.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.poemtest.Activity.BottomNavigationViewActivity;
import com.example.poemtest.R;
import com.example.poemtest.index.dialog.TestGradeDialog;
import com.example.poemtest.index.entity.PoetryTakeBean;
import com.example.poemtest.index.util.PoetryManage;
import com.example.poemtest.utils.FullScreenUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 考一考页面
 */
public class TakeExamActivity extends AppCompatActivity {

    List<PoetryTakeBean> list = new ArrayList<>();
    int index = 0;
    private TextView tv_name,tv_show,tv_show1;
    private EditText edit_1,edit;
    PoetryTakeBean bean;
    private int count = 0;
    private Button button;
    private StringBuffer buffer = new StringBuffer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_exam);
        FullScreenUtil.getInstance().fullScreen(this,true);

        findViewById(R.id.back).setOnClickListener(view -> {
            finish();
        });

        list = PoetryManage.getInstance().getRandomlyTakeBean();
//        RecyclerView recyclerView = findViewById(R.id.recyclerView);
//        TakeExamAdapter adapter = new TakeExamAdapter(PoetryManage.getInstance().getRandomlyItems());
//        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        recyclerView.setAdapter(adapter);
//        adapter.notifyDataSetChanged();

        tv_name =findViewById(R.id.tv_name);
        tv_show =findViewById(R.id.tv_show);
        tv_show1 =findViewById(R.id.tv_show1);
        edit_1 =findViewById(R.id.edit_1);
        edit =findViewById(R.id.edit);

        bean = list.get(index);
        tv_name.setText(bean.getTitle());
        tv_show.setText(bean.getContent1());
        tv_show1.setText(bean.getContent3());

        button = findViewById(R.id.button);
        button.setText("下一题" + "(" + (index +1) + "/" + list.size() + ")");
        button.setOnClickListener(v -> {
            int c = 0;
            if (bean != null){
                if (!TextUtils.isEmpty(edit.getText().toString())){
                    if (edit.getText().toString().equalsIgnoreCase(bean.getContent2())){
                        count += 5;
                        c = 5;
                    }
                }
                if (!TextUtils.isEmpty(edit_1.getText().toString())){
                    if (edit_1.getText().toString().equalsIgnoreCase(bean.getContent4())){
                        count += 5;
                        c += 5;
                    }
                }
            }

            buffer.append("第"+(index + 1) + "题").append(c).append("分");
            index ++;
            if (index%2 == 0){
                buffer.append("\n");
            }else {
                buffer.append("   ");
            }

            if (index < list.size()) {
                bean = list.get(index);
                tv_name.setText(bean.getTitle());
                tv_show.setText(bean.getContent1());
                tv_show1.setText(bean.getContent3());
                edit.setText("");
                edit_1.setText("");
                if (list.size()-index == 1){
                    button.setText("提交");
                }else {
                    button.setText("下一题" + "(" + (index +1) + "/" + list.size() + ")");
                }
            }else {
                buffer.append("\n").append("总分：").append(count).append("分").append("\n");
                TestGradeDialog testGradeDialog = new TestGradeDialog(this);

                testGradeDialog.show();
                TextView textView = testGradeDialog.findViewById(R.id.text);
                textView.setText(buffer.toString());
                Button button = testGradeDialog.findViewById(R.id.btn);
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        testGradeDialog.cancel();
                        finish();
                    }
                });
            }
        });
    }
}